package com.buercorp.wangyu.core1.ch9.map;

import java.util.*;
import java.util.function.BiConsumer;

/**
 * This program demonstrates the use of a map with key type String and value type Employee.
 *
 * @author Cay Horstmann
 * @version 1.12 2015-06-21
 */
public class MapTest {
    public static void main(String[] args) {
        var staff = new HashMap<String, Employee>();
        // put 添加
        staff.put("144-25-5464", new Employee("Amy Lee"));
        staff.put("567-24-2546", new Employee("Harry Hacker"));
        staff.put("157-62-7935", new Employee("Gary Cooper"));
        staff.put("456-62-5527", new Employee("Francesca Cruz"));

        // print all entries

        System.out.println("集合map中的数据" + staff);

        // remove an entry

        Employee st = staff.remove("567-24-2546");
        System.out.println("删除的" + st);
        System.out.println("=============================");
        // replace an entry
        // 替换
        Employee st1 = staff.put("456-62-5527", new Employee("Francesca Miller"));
        System.out.println("替换的是：" + st1);

        System.out.println("=============================");
        // look up a value
        // 查找
        System.out.println("查找" + staff.get("157-62-7935"));

        // iterate through all entries
        System.out.println("=============================");
        // 遍历
        staff.forEach(new BiConsumer<String, Employee>() {
            @Override
            public void accept(String s, Employee employee) {
                System.out.println("key=" + s + ", value=" + employee);
            }
        });
        System.out.println("=============================");
        staff.forEach((k, v) ->
                System.out.println("key=" + k + ", value=" + v));
        /*
        * key=157-62-7935, value=[name=Gary Cooper, salary=0.0]
          key=144-25-5464, value=[name=Amy Lee, salary=0.0]
          key=456-62-5527, value=[name=Francesca Miller, salary=0.0]
        * */

        // 迭代器遍历键（KeySet） 值（values）
        Iterator<String> it = staff.keySet().iterator();
        while (it.hasNext()) {
            String key = it.next();
//            String key = (String) it.next();
            System.out.println("key:" + key);
        }

        /*import java.util.*;

        public class Main {
            public static void main(String[] args) {
                // 创建一个Map实例
                Map<String, Integer> staff = new HashMap<>();
                staff.put("John", 25);
                staff.put("Jane", 30);
                staff.put("Tom", 35);

                // 使用keySet和iterator遍历所有键
                Iterator<String> iterator = staff.keySet().iterator();
                while (iterator.hasNext()) {
                    String key = iterator.next();
                    System.out.println("Key: " + key);
                }
            }
        }*/

    }
}
